app.controller('goodsCtrl',['$scope','$state','$cookies','goodsService','utilService','toaster','$modal',function($scope,$state,$cookies,goodsService,utilService,toaster,$modal){
    $scope.brands;
    $scope.types;

    $scope.goodsListVO={};

    //查询商品列表
    $scope.findPageAll=function(){
        utilService.queryForPagingList($scope,{
            url:$scope.app.domain + 'chain/goods',
            params:$scope.goodsListVO
        }).then(function(result){
        });
    }

    $scope.findPageAll();

    //查询品牌信息
    $scope.querybrands=function(){
        goodsService.querybrands($scope).then(function (result) {
            switch (result.status){
                case 200:
                    $scope.brands=result.data;
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })
    }

    $scope.querybrands();

    //查询类型信息
    $scope.queryType=function(){
        goodsService.queryType($scope).then(function (result) {
            switch (result.status){
                case 200:
                    $scope.types=result.data;
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })
    }

    $scope.queryType();

    //批量上架
    $scope.batchUpdateUpStatus=function(){
        goodsService.batchUpdateUpStatus($scope).then(function (result) {
            switch (result.status){
                case 200:
                    $scope.types=result.data;
                    $scope.findPageAll();
                    toaster.pop('success', "提示", "操作成功");
                    break;
                case 677:
                case 678:
                    toaster.pop('waiting', "提示", result.message);
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })
    }

    //批量下架
    $scope.batchUpdateDownStatus=function(){
        goodsService.batchUpdateDownStatus($scope).then(function (result) {
            switch (result.status){
                case 200:
                    $scope.types=result.data;
                    $scope.findPageAll();
                    toaster.pop('success', "提示", "操作成功");
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })
    }


    //更新上下架,申请再次审核
    $scope.updateStatus=function(id,status){
        debugger;
        goodsService.updateStatus($scope,id,status).then(function (result) {
            switch (result.status){
                case 200:
                    $scope.types=result.data;
                    $scope.findPageAll();
                    toaster.pop('success', "提示", "操作成功");
                    break;
                case 695:
                case 704:
                case 705:
                    toaster.pop('waiting', "提示", result.message);
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })

    }

    //批量选择
    $scope.selected=[];

    $scope.isChecked = function(item){
        return $scope.selected.indexOf(item) >= 0 ;
    } ;

    $scope.updateSelection = function($event,item){

        var checkbox = $event.target ;
        var checked = checkbox.checked ;

        if(checked){
            $scope.selected.push(item) ;
        }else{
            var idx = $scope.selected.indexOf(item) ;
            $scope.selected.splice(idx,1) ;
        }
    };

    $scope.isCheckedAll=function(){

        if($scope.list==null){
            return;
        }

        return $scope.selected.length==$scope.list.length;
    }

    $scope.checkAll=function($event){
        var checkbox = $event.target ;
        var checked = checkbox.checked ;

        for(var i=0;i<$scope.list.length;i++){
            if(checked){
                $scope.selected.push($scope.list[i]);
            }else{
                $scope.selected.splice(0,$scope.selected.length);
            }
        }
    }

    $scope.initSelect=function(){
        $scope.selected=[];
    }


    $scope.deleteGoods = function(id, name){
        var param = {
            id:id,
            name:name
        };

        openModal("你是否要删除[" + name + "]商品？删除后将无法恢复");

        function openModal(msg){
            utilService.modal($modal, $scope, {
                title:"删除商品",
                controller:"deleteGoodsCtrl",
                message:msg,
                data:param
            });
        }
    }

}]);

app.controller('deleteGoodsCtrl', ['$scope', '$modalInstance','$state','goodsService','toaster','modalData',
    function($scope, $modalInstance,$state,goodsService,toaster,modalData) {
        $scope.modal = modalData;

        $scope.goods = {
            id: modalData.data.id
        };
        $scope.isDelModal=true;

        //确认删除
        $scope.ok = function(){
            if(!$scope.isDelModal){
                return;
            }

            $scope.isDelModal = false;

            goodsService.delete($scope).then(function(result){
                switch (result.status){
                    case 200:
                        $modalInstance.dismiss('success');
                        $scope.findPageAll();
                        toaster.pop('success', "提示", "操作成功");
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }
            });
        }

        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }
]);